package localizer.main;

import java.util.ArrayList;

import android.app.Application;
import android.content.Context;


public class Config {

	private String username;
	private String id;
	private static Config instancia = null;

	private Config(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		if(bdHelper.hasUser()){
			username = bdHelper.getUsername();
			id = bdHelper.getId();
		}
		bdHelper.close();
		bdHelper = null;
	}

	public static Config getInstance(Context context){
		if(instancia == null){
			instancia = new Config(context);
		}
		return instancia;
	}

	public String getUsername(Context context){
		if ( username == null){
			BdAdabter bdHelper = new BdAdabter(context) ;
			bdHelper.open();
			username = bdHelper.getUsername();
			bdHelper.close();
			bdHelper = null;
		}
		return username;
	}

	public String getID(Context context){
		if( id == null){
			BdAdabter bdHelper = new BdAdabter(context) ;
			bdHelper.open();
			id = bdHelper.getId();
			bdHelper.close();
			bdHelper = null;
		}
		return id;
	}
	
	public void createUser(Context context, String login, String id){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.createUser(login, id);
		this.id = id;
		this.username = login;
		bdHelper.close();
		bdHelper = null;
	}
	
	public String getLat(Context context){
		String vari;
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		vari = bdHelper.getLat();
		bdHelper.close();
		bdHelper = null;
		return vari;
	}

	public String getLong(Context context){
		String vari;
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		vari = bdHelper.getLong();
		bdHelper.close();
		bdHelper = null;
		return vari;
	}
	
	public String getPhone(Context context){
		String vari;
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		vari = bdHelper.getPhone();
		bdHelper.close();
		bdHelper = null;
		return vari;
	}
	
	public String getData(Context context){
		String vari;
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		vari = bdHelper.getData();
		bdHelper.close();
		bdHelper = null;
		return vari;
	}
	
	public void createLoc(Context context, String lat, String longi, String phone, String data){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableLocalization();
		bdHelper.createLoc(lat, longi,phone, data);
		bdHelper.close();
		bdHelper = null;
	}
	
	public boolean hasLoc(Context context) {
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		boolean has = bdHelper.hasLoc();
		bdHelper.close();
		bdHelper = null;
		return has;
	}
	
	public void createTimeLoc(Context context, String time){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableLoc();
		bdHelper.createTimeLoc(time);
		bdHelper.close();
		bdHelper = null;
	}
	
	public void createTimeTask(Context context, String time){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableTask();
		bdHelper.createTimeTask(time);
		bdHelper.close();
		bdHelper = null;
	}
	
	public void createTimeRing(Context context, String time){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableRing();
		bdHelper.createTimeRing(time);
		bdHelper.close();
		bdHelper = null;
	}
	
	public boolean hasUser(Context context) {
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		boolean has = bdHelper.hasUser();
		bdHelper.close();
		bdHelper = null;
		return has;
	}

	public String getTimeRing(Context application) {
		BdAdabter bdHelper = new BdAdabter(application) ;
		bdHelper.open();
		String has = bdHelper.getTimeRing();
		bdHelper.close();
		bdHelper = null;
		return has;
	}

	public String getTimeTask(Context application) {
		BdAdabter bdHelper = new BdAdabter(application) ;
		bdHelper.open();
		String has = bdHelper.getTimeTask();
		bdHelper.close();
		bdHelper = null;
		return has;
	}

	public String getTimeLoc(Context application) {
		BdAdabter bdHelper = new BdAdabter(application) ;
		bdHelper.open();
		String has = bdHelper.getTimeLoc();
		bdHelper.close();
		bdHelper = null;
		return has;
	}
	public String getPhoneNumber(Context application) {
		BdAdabter bdHelper = new BdAdabter(application) ;
		bdHelper.open();
		String has = bdHelper.getPhoneNumber();
		bdHelper.close();
		bdHelper = null;
		return has;
	}

	public String getPassword(Context applicationContext) {
		BdAdabter bdHelper = new BdAdabter(applicationContext) ;
		bdHelper.open();
		String has = bdHelper.getPassword();
		try {
			has = SimpleCrypto.decrypt("Master-Password", has);
		} catch (Exception e) {
		}
		bdHelper.close();
		bdHelper = null;
		return has;
	}
	
	public void createPassword(Context context, String password){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTablePassword();
		try {
			password = SimpleCrypto.encrypt("Master-Password", password);
		} catch (Exception e) {
		}
		bdHelper.createPassword(password);
		bdHelper.close();
		bdHelper = null;
	}
	
	public void createPhoneNumber(Context context,String phoneNumber){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTablePhoneNumber();
		
		bdHelper.createPhoneNumber(phoneNumber);
		bdHelper.close();
		bdHelper = null;
	}
	
	
	
	public boolean isEnableCheckUpdateLocation(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableCheckUpdateLocation();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	public void setEnableCheckUpdateLocation(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableEnableCheckUpdateLocation();
		bdHelper.createEnableCheckUpdateLocation(b?1:0);
		bdHelper.close();
		
	}
	
	public boolean isEnableUpdateTask(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableUpdateTask();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	public void setEnableUpdateTask(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropEnableUpdateTask();
		bdHelper.createEnableUpdateTask(b?1:0);
		bdHelper.close();
	}
	
	public boolean isEnableCheckRing(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableCheckRing();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	public void setEnableCheckRing(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableEnableCheckRing();
		bdHelper.createEnableCheckRing(b?1:0);
		bdHelper.close();
	}
	
	
	public boolean isEnableCheckRingByMsg(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableCheckRingByMsg();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	
	public boolean isEnableCheckCheckSim(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableCheckCheckSim();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	public void setEnableCheckRingByMsg(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableEnableCheckRingByMsg();
		bdHelper.createEnableCheckRingByMsg(b?1:0);
		bdHelper.close();
	}
	
	public void setEnableCheckCheckSim(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableEnableCheckCheckSim();
		bdHelper.createEnableCheckCheckSim(b?1:0);
		bdHelper.close();
	}
	
	
	public boolean isEnableCheckLocByMsg(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		int i = bdHelper.getEnableCheckLocByMsg();
		bdHelper.close();
		boolean resp=(i==1)?true:false;
		return resp;
	}
	public void setEnableCheckLocByMsg(Context context,boolean b){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.dropTableEnableCheckLocByMsg();
		bdHelper.createEnableCheckLocByMsg(b?1:0);
		bdHelper.close();
	}
	
	public void addSimId(Context context,String simId){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		ArrayList<String> ret = bdHelper.getAllSimId();
		if(!ret.contains(simId)){
			bdHelper.addSimid(simId);
		}
		bdHelper.close();
	}
	
	public ArrayList<String> getSimIds(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		ArrayList<String> ret = bdHelper.getAllSimId();
		bdHelper.close();
		return ret;
		
	}
	
	public boolean hasSimId(Context context,String simId){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		ArrayList<String> ret = bdHelper.getAllSimId();
		bdHelper.close();
		return ret.contains(simId);
	}
	
	public String getEmergencyPhone(Context context){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		String resp = bdHelper.getEmergencyPhone();
		bdHelper.close();
		return resp;
	}
	
	public void addEmergencyPhone(Context context,String phone){
		BdAdabter bdHelper = new BdAdabter(context) ;
		bdHelper.open();
		bdHelper.addEmergencyPhone(phone);
		bdHelper.close();
	}
	
	


}
